package com.ofek.eventos.notyet;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

import com.ofek.applicationbase.utils.Log;
import com.ofek.eventos.EventGroup;
import com.ofek.eventos.R;
import com.ofek.eventos.db.datatypes.EventData;

public class CategorizedFragment extends Fragment {
	public interface EventsFragmentController {
		public void onShowing(Fragment frag);
		public void onEventPressed(EventData event);
	}
	private ExpandableListView mExpandableListView;
	private boolean expanded;
	public CategorizedListAdapter listAdapter;
	protected EventGroup eventGroup;
	private View progress;
	private ArrayList<EventGroup> eventsToShow;
	private boolean editState;
	private boolean waiting;

	public static final CategorizedFragment newInstance() {
		final CategorizedFragment f = new CategorizedFragment();
		final Bundle bdl = new Bundle(3);
		f.setArguments(bdl);
		return f;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		Log.d("onCreate");
		super.onCreate(savedInstanceState);
		expanded = true;
	}



	@Override
	public View onCreateView(final LayoutInflater inflater,
			final ViewGroup container, final Bundle savedInstanceState) {
		final View inflate = inflater.inflate(R.layout.diary_layout, container,
				false);
		progress = inflate.findViewById(R.id.progress);
		progress.setVisibility(waiting ? View.VISIBLE : View.GONE);
		//mExpandableListView = (ExpandableListView) inflate
		//		.findViewById(R.id.expandable_list);


		mExpandableListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(final ExpandableListView parent, final View v,
					final int groupPosition, final int childPosition, final long id) {
				final EventsFragmentController activity = (EventsFragmentController) CategorizedFragment.this.getActivity();
				final EventData event=(EventData) listAdapter.getChild(groupPosition, childPosition);
				activity.onEventPressed(event);
				return true;
			}
		});
		if (eventsToShow==null) return inflate; 
		mExpandableListView.setAdapter(listAdapter = new CategorizedListAdapter(getActivity(), eventsToShow));
		if (expanded)
			mExpandableListView.expandGroup(0);
		listAdapter.setEditState(editState);
		return inflate;
	}
	@Override
	public void onResume() {
		super.onResume();
		final EventsFragmentController activity = (EventsFragmentController)getActivity();
		activity.onShowing(this);
	}

	public void setWaitingState(final boolean waiting) {
		this.waiting = waiting;
		if ( progress!=null ) 
			progress.setVisibility(waiting ? View.VISIBLE : View.GONE);
	}

	public void setEventsToShow(final ArrayList<EventGroup> events) {
		eventsToShow=events;
		setWaitingState(false);
		if (eventsToShow==null || mExpandableListView==null) return; 
		mExpandableListView.setAdapter(listAdapter = new CategorizedListAdapter(getActivity(), eventsToShow));
		if (expanded)
			mExpandableListView.expandGroup(0);
		listAdapter.setEditState(editState);
	}

	public void setEdit(final boolean edit) {	
		editState = edit;
		if (isResumed()) {
			listAdapter.setEditState(edit);
		}
	}
}